﻿using System;
using System.Collections.Generic;
using System.Data.Entity; // needed for lambda includes
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace TheCup.ApiControllers
{

    public class TeeController : ApiController
    {
        TheCupContext ctx = new TheCupContext();

        public IEnumerable<Tee> Get()
        {
            return (from tee in ctx.Tees
                    select tee).OrderBy(c => c.Name).AsEnumerable();
        }

        public Tee Get(int id)
        {
            Tee e = new Tee();
            e = (from tee in ctx.Tees.Include(t => t.Course)
                 where tee.Id.Equals(id)
                 select tee).FirstOrDefault();
            return e;
        }

        [NonAction]
        public IEnumerable<Tee> GetForCourse(int id)
        {
            return (from tee in ctx.Tees
                     where tee.CourseId == id && tee.IsActive
                     orderby tee.Created descending
                     select tee).AsEnumerable();
        }

        //[Route("api/tee/getbygender")]
        public IEnumerable<Tee> GetByGender(int playerid, int courseid)
        {
            var p = ctx.Players.Find(playerid);

            return (from tee in ctx.Tees
                    where tee.Gender == p.Sex.ToString() && tee.CourseId == courseid && tee.IsActive
                    orderby tee.Created descending
                    select tee).AsEnumerable();
        }

        public Tee Post([FromBody]Tee e)
        {
            e.ApplyRules();
            ctx.Tees.Add(e);
            ctx.SaveChanges();
            return e;
        }

        public void Put(int id, [FromBody]Tee e)
        {
            e.ApplyRules();

            Tee tee = this.Get(e.Id);
            tee.Name = e.Name;
            tee.IsActive = e.IsActive;
            tee.Description = e.Description;
            tee.Gender = e.Gender;

            tee.Slope = e.Slope;
            tee.CR = e.CR;

            tee.Par1 = e.Par1;
            tee.Par2 = e.Par2;
            tee.Par3 = e.Par3;
            tee.Par4 = e.Par4;
            tee.Par5 = e.Par5;
            tee.Par6 = e.Par6;
            tee.Par7 = e.Par7;
            tee.Par8 = e.Par8;
            tee.Par9 = e.Par9;
            tee.Par10 = e.Par10;
            tee.Par11 = e.Par11;
            tee.Par12 = e.Par12;
            tee.Par13 = e.Par13;
            tee.Par14 = e.Par14;
            tee.Par15 = e.Par15;
            tee.Par16 = e.Par16;
            tee.Par17 = e.Par17;
            tee.Par18 = e.Par18;

            tee.Idx1 = e.Idx1;
            tee.Idx2 = e.Idx2;
            tee.Idx3 = e.Idx3;
            tee.Idx4 = e.Idx4;
            tee.Idx5 = e.Idx5;
            tee.Idx6 = e.Idx6;
            tee.Idx7 = e.Idx7;
            tee.Idx8 = e.Idx8;
            tee.Idx9 = e.Idx9;
            tee.Idx10 = e.Idx10;
            tee.Idx11 = e.Idx11;
            tee.Idx12 = e.Idx12;
            tee.Idx13 = e.Idx13;
            tee.Idx14 = e.Idx14;
            tee.Idx15 = e.Idx15;
            tee.Idx16 = e.Idx16;
            tee.Idx17 = e.Idx17;
            tee.Idx18 = e.Idx18;

            tee.L1 = e.L1;
            tee.L2 = e.L2;
            tee.L3 = e.L3;
            tee.L4 = e.L4;
            tee.L5 = e.L5;
            tee.L6 = e.L6;
            tee.L7 = e.L7;
            tee.L8 = e.L8;
            tee.L9 = e.L9;
            tee.L10 = e.L10;
            tee.L11 = e.L11;
            tee.L12 = e.L12;
            tee.L13 = e.L13;
            tee.L14 = e.L14;
            tee.L15 = e.L15;
            tee.L16 = e.L16;
            tee.L17 = e.L17;
            tee.L18 = e.L18;

            ctx.Entry(tee).State = EntityState.Modified;
            ctx.SaveChanges();
        }

        public void Delete(int id)
        {
            Tee tee = this.Get(id);
            if (tee != null)
            {
                ctx.Tees.Remove(tee);
                ctx.SaveChanges();
            }
        }
    }

}